﻿using System;
using System.Collections.Generic;
using System.Text;

using System.IO;

using System.Security.Cryptography;

namespace WindowsWeb
{
    class SecurityHelper
    {
        private static SecurityHelper _Current;
        public static SecurityHelper Current
        {
            get
            {
                if (_Current == null)
                {
                    _Current = new SecurityHelper();
                }
                return _Current;
            }
        }

        private string key = "www.nikoyo.com";
        /// <summary>
        /// 加密字符串
        /// </summary>
        /// <param name="inputStr">输入字符串</param>
        /// <returns>加密结果</returns>
        public string EncryptString(string inputStr)
        {
            DESCryptoServiceProvider des = CreateDESC(key);
            byte[] inputByteArray = Encoding.Default.GetBytes(inputStr);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            ms.Close();
            des.Clear();
            return ret.ToString();
        }
        /// <summary>
        /// 解密字符串
        /// </summary>
        /// <param name="inputStr">加密了的字符串</param>
        /// <returns>解密结果</returns>
        public string DecryptString(string inputStr)
        {
            DESCryptoServiceProvider des = CreateDESC(key);
            byte[] inputByteArray = new byte[inputStr.Length / 2];
            for (int x = 0; x < inputStr.Length / 2; x++)
            {
                int i = (Convert.ToInt32(inputStr.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            string str = System.Text.Encoding.Default.GetString(ms.ToArray());
            ms.Close();
            des.Clear();
            return str;
        }

        private DESCryptoServiceProvider CreateDESC(string key)
        {
            byte[] sKey;
            byte[] sIV;
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            byte[] keyByteArray = Encoding.Default.GetBytes(key);
            SHA1 ha = new SHA1Managed();
            byte[] hb = ha.ComputeHash(keyByteArray);
            ha.Clear();
            int ii = hb.Length;
            sKey = new byte[8];
            sIV = new byte[8];
            for (int i = 0; i < 8; i++)
                sKey[i] = hb[i];
            for (int i = 8; i < 16; i++)
                sIV[i - 8] = hb[i];
            des.Key = sKey;
            des.IV = sIV;
            return des;
        }
    }
}
